# -*- coding: utf-8 -*-
__author__ = 'wwj'
import sys
# reload(sys)
# sys.setdefaultencoding('utf-8')
from celery import Celery
import config
import sys
import os
from celery.utils.log import get_task_logger
# from celery_once import QueueOnce
import pymongo
from datetime import *
# app = Celery('dispatch',include=['tasks.xvideo'])
app = Celery('dispatch')
app.config_from_object(config)
import hashlib
import youtube_dl
import requests
from subprocess import check_call
import scrapy
import re
from gridfs import *
logger = get_task_logger(__name__)
import glob


 # soft_time_limit=5
@app.task(bind=True,queue="xvideo",max_retries=1,routing_key='xvideo')
def down(self,url):
    # import ipdb;ipdb.set_trace()
    logger.info("Calling task add")
    client = pymongo.MongoClient("47.74.21.140",connect=False)
    client.admin.authenticate("admin321", "dsf::6666,,<<", mechanism='SCRAM-SHA-1')
    key = hashlib.md5(url.encode("utf8")).hexdigest()
    db = client.photo
    # fs = GridFSBucket(db)
    fs = GridFS(db)
    if not fs.exists(filename=key):
        print url
        ydl_opts = {}
        ydl_opts["outtmpl"] = '/mnt/video/{}'.format(key)
        command = "/usr/bin/youtube-dl {} --output {}".format(url,ydl_opts["outtmpl"]+".%(ext)s")
        if check_call(command.split(), shell=False) == 0:
            # fs.upload_from_stream(key,obj,chunk_size_bytes=10485760)
            name = glob.glob(ydl_opts["outtmpl"] + ".*")[0]
            obj = open(name)
            fs.put(obj,filename=name)
        # return {"key":key,"url":url}



